<?php

namespace app\admin\model;

use think\Model;

class AdminAdminLog extends Model
{

    protected $table = 'admin_admin_log';

    public static function record()
    {
        if (!$desc = Request()->except(['s', '_pjax'])) {
            return;
        }
        if (isset($desc['page']) && isset($desc['limit'])) {
            return;
        }
        foreach ($desc as $k => $v) {
            if (is_string($v) && strlen($v) > 255 || stripos($k, 'password') !== false) {
                unset($desc[$k]);
            }
        }
        $info = [
            'uid'        => session('admin')['id'] ?? 0,
            'url'        => Request()->url(),
            'desc'       => json_encode($desc),
            'ip'         => Request()->getRealIp(),
            'user_agent' => Request()->header('User-Agent', ''),
        ];
        $res = self::where('uid', $info['uid'])
            ->order('id', 'desc')
            ->find();
        if (isset($res['url']) !== $info['url']) {
            self::create($info);
        }
    }

    // 管理员日志记录

    public function log()
    {
        return $this->belongsTo('AdminAdmin', 'uid', 'id');
    }
}
